//
//  SWWeiboCell.m
//  SinaWeather
//
//  Created by nan4 li on 12-5-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "SWWeiboCell.h"
#import "ThumbImageView.h"
#import "UIColorCategory.h"
#define HEAD_WIDTH      35
#define THUMB_WIDTH     200
#define STATIS_WIDTH    300
#define X_GAP0          10
#define Y_GAP0          5
@interface SWWeiboCell()
{
    BOOL isThumbLoaded;
}
@end

@implementation SWWeiboCell
@synthesize statusMode;
@synthesize repostImage;

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.repostImage = [UIImage imageNamed:@"periphery_snapphoto_forward.png"];//[[SWImageCache sharedImageCache] imageForKey:@"periphery_snapphoto_forward.png"];
    }
    return  self;
}
-(void)dealloc
{
    [thumbImage release],thumbImage = nil;
    statusMode.delegate = nil;
    self.statusMode = nil;
    self.repostImage = nil;
    [super dealloc];
}

-(void)useStatus: (SWStatusModel*)mode
{
    self.statusMode = mode;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect
{
    
    UIFont *screenNameFont = [UIFont systemFontOfSize:14.0];
    UIFont *statusFont = [UIFont systemFontOfSize:11.0];
    
    
    //头像
    if (statusMode.headImage==nil) {
        UIImage *hmage = [UIImage imageNamed:@"periphery_snapphoto_avatar_default.png"];//[[SWImageCache sharedImageCache] imageForKey:@"periphery_snapphoto_avatar_default.png"];
        [hmage drawInRect:statusMode.rectHeadImage blendMode:kCGBlendModeNormal alpha:1.0f];
    }
    else {
        [statusMode.headImage drawInRect:statusMode.rectHeadImage blendMode:kCGBlendModeNormal alpha:1.0f];
    }
    
    [[UIColor colorWithHexString:@"7390A2"] set];
    //昵称
    [statusMode.nickName drawInRect:statusMode.rectNickName withFont:screenNameFont 
               lineBreakMode:UILineBreakModeTailTruncation];

    [[UIColor colorWithHexString:@"A1A1A1"] set];
     //位置
     [statusMode.location drawInRect:statusMode.rectLocation withFont:statusFont 
                       lineBreakMode:UILineBreakModeTailTruncation];
    
    //时间
    [statusMode.time drawInRect:statusMode.rectTime withFont:statusFont lineBreakMode:UILineBreakModeTailTruncation];
    
    [[UIColor colorWithHexString:@"727272"] set];
    //博文
    [statusMode.text drawInRect:statusMode.rectText withFont:statusFont 
                      lineBreakMode:UILineBreakModeTailTruncation];
    
    //缩略图
    if (statusMode.thumbImage==nil) {
        UIImage *hmage = [UIImage imageNamed:@"periphery_snapphoto_photo_none.png"];//[[SWImageCache sharedImageCache] imageForKey:@"periphery_snapphoto_photo_none.png"];
        [hmage drawInRect:statusMode.rectThumbImage blendMode:kCGBlendModeNormal alpha:1.0f];
        isThumbLoaded = FALSE;
    }
    else {
        [statusMode.thumbImage drawInRect:statusMode.rectThumbImage blendMode:kCGBlendModeNormal alpha:1.0f];
        isThumbLoaded = TRUE;
    }
    
    //转发按钮
    [[UIColor whiteColor] set];
    [repostImage drawInRect:statusMode.rectRepostNum];
    
    //转发数字
    CGSize numTextSize = [statusMode.repostNum sizeWithFont:statusFont constrainedToSize:CGSizeMake(statusMode.rectRepostNum.size.width-25, 0) lineBreakMode:UILineBreakModeTailTruncation];
    CGRect rectNum = CGRectMake(statusMode.rectRepostNum.origin.x+25, statusMode.rectRepostNum.origin.y+(statusMode.rectRepostNum.size.height-numTextSize.height)/2, 
                                statusMode.rectRepostNum.size.width-25, statusMode.rectRepostNum.size.height);
    [statusMode.repostNum drawInRect:rectNum withFont:statusFont 
                      lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
    
    //分隔线
    UIImage *lineImage = [UIImage imageNamed:@"periphery_snapphoto_message_curt.png"];//[[SWImageCache sharedImageCache] imageForKey:@"periphery_snapphoto_message_curt.png"];
    [lineImage drawInRect:statusMode.rectSepreateLine];    
}

-(void)repostButtonPressed
{
    if([statusMode.delegate respondsToSelector:@selector(repostWeibo:)])
        [statusMode.delegate repostWeibo:statusMode.statusId];
    
    self.repostImage = [UIImage imageNamed:@"periphery_snapphoto_forward.png"];//[[SWImageCache sharedImageCache] imageForKey:@"periphery_snapphoto_forward.png"];
    [self setNeedsDisplayInRect:statusMode.rectRepostNum];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint pt = [[touches anyObject] locationInView:self];  
    if (CGRectContainsPoint(statusMode.rectRepostNum, pt)) {        
        self.repostImage = [UIImage imageNamed:@"periphery_snapphoto_forward_press.png"];//[[SWImageCache sharedImageCache] imageForKey:@"periphery_snapphoto_forward_press.png"];
        [self setNeedsDisplayInRect:statusMode.rectRepostNum];
        [self performSelector:@selector(repostButtonPressed) withObject:nil afterDelay:0.3];

    }
    else if(CGRectContainsPoint(statusMode.rectThumbImage, pt)) {
        if (!isThumbLoaded) {
            return;
        }
        
        if ([statusMode.delegate respondsToSelector:@selector(thumbImageClicked:)]) {
            [statusMode.delegate thumbImageClicked:statusMode.orgnalUrl];
        }
    }
         
}

@end
